package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Goods;
import com.ruoyi.common.core.domain.entity.Order;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.IGoodsService;
import com.ruoyi.system.service.IOrderService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2021-11-15
 */
@Controller
@RequestMapping("/system/goods")
public class GoodsController extends BaseController {
    private String prefix = "system/goods";

    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IOrderService orderService;

    //    @RequiresPermissions("system:goods:view")
    @GetMapping()
    public String goods() {
        return prefix + "/goods";
    }


    //    @RequiresPermissions("system:goods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Goods goods) {
        startPage();
        List<Goods> list = goodsService.selectGoodsList(goods);
        return getDataTable(list);
    }


    @RequiresPermissions("system:goods:export")
    @Log(title = "导出商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Goods goods) {
        List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }


    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }


    @RequiresPermissions("system:goods:add")
    @Log(title = "添加商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Goods goods) {
        return toAjax(goodsService.insertGoods(goods));
    }


    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Goods goods = goodsService.selectGoodsById(id);
        mmap.put("goods", goods);
        return prefix + "/edit";
    }

    @GetMapping("/buyGoods/{id}")
    public String findGoodsMessage(@PathVariable("id") Long id, ModelMap mmap) {
        Goods goods = goodsService.selectGoodsById(id);
        mmap.put("goods", goods);
        return prefix + "/buyGoods";
    }


    @RequiresPermissions("system:goods:edit")
    @Log(title = "编辑商品信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Goods goods) {
        return toAjax(goodsService.updateGoods(goods));
    }


    @RequiresPermissions("system:goods:remove")
    @Log(title = "删除商品", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(goodsService.deleteGoodsByIds(ids));
    }


    @PostMapping("/buy")
    @ResponseBody
    public AjaxResult buyGoods(Order order) throws ParseException {
        SysUser currentUser = ShiroUtils.getSysUser();
        String name = currentUser.getLoginName();
        System.out.println(currentUser);
        Date date = new Date();
        order.setTime(date);
        Goods goods = goodsService.selectGoodsById(order.getId());

        if (goods.getGoodNum() - order.getOrderNum() < 0)
            return AjaxResult.error("库存不足，下单失败");
        else
            goods.setGoodNum(goods.getGoodNum() - order.getOrderNum());
        BigDecimal price = goods.getPrice();
        BigDecimal orderNum = new BigDecimal(order.getOrderNum().toString());
        BigDecimal totalPrice = price.multiply(orderNum);
        order.setPrice(totalPrice);

        order.setRole(name);
        List orderList = orderService.selectOrderList(null);
        order.setId((long) orderList.size() + 1);
        orderService.insertOrder(order);
        goodsService.updateGoods(goods);
        return AjaxResult.success("下单成功");
    }
}
